package com.ymatou.shoppingcart.api.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.shoppingcart.api.parameter.BatchDeleteCatalogFromCartBean;
import com.ymatou.shoppingcart.api.service.BatchDeleteCatalogFromCartCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.ShoppingCartCallServices;
import com.ymttest.database.mongo.ShopCartMongoBean;
import com.ymttest.database.mongo.ShoppingCartMongoWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 根据CartId列表删除购物车中记录 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BatchDeleteCatalogFromCart {
	private static BatchDeleteCatalogFromCartBean batchbean;
	private static BatchDeleteCatalogFromCartCall batchcall;
	ShoppingCartMongoWapper scw;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据CartId列表删除购物车中记录");
	}

	@Before
	public void caseUp() {
		batchbean = new BatchDeleteCatalogFromCartBean();
		batchcall = new BatchDeleteCatalogFromCartCall();
		try {
			scw = new ShoppingCartMongoWapper();
		} catch (Exception e) {
			Logger.setup("MongoDB初始化失败：" + e.getMessage(), -1);
		}
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	//TODO 购物车表和结构都有修改，case需要对应调整

	/*@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchDeleteCatalogFromCart_001() {
		Logger.start(true,"根据CartId列表删除购物车中记录成功");
		try {
			Logger.comment("从mongodb中获取一条购物车记录");

			ShopCartMongoBean re = scw.getOneShoppingCartInfo("{}");
			int count = scw.getShoppingCartCount("{\"uid\":\"" + re.getUid() + "\"}");
			Logger.comment(re.getUid() + "用户记录条数：" + count);
			List<String> CartIdList = new ArrayList<String>();
			Logger.comment("把cartid加入list：" + re.get_id());
			CartIdList.add(re.get_id());
			Logger.comment("setCartIdList:" + CartIdList);
			batchbean.setCartIdList(CartIdList);
			Logger.comment("setUserId:" + re.getUid());
			batchbean.setUserId(re.getUid());
			batchcall.setData(batchbean);
			batchcall.callService();
			Logger.comment("delcatcall send:" + batchcall.getEntityString());
			Logger.comment("delcatcall return:" + batchcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, batchcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, batchcall.getBCode(), "验证返回bcode");
			// 删除了一个记录
			Logger.verifyEquals(count - 1, scw.getShoppingCartCount("{\"uid\":\"" + re.getUid() + "\"}"), "验证删除后记录条数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchDeleteCatalogFromCart_002() {
		Logger.start(true,"不输入必填项CartIdList，删除购物车中记录失败");
		try {
			Logger.comment("从mongodb中获取一条购物车记录");

			ShopCartMongoBean re = scw.getOneShoppingCartInfo("{}");
			int count = scw.getShoppingCartCount("{\"uid\":\"" + re.getUid() + "\"}");
			Logger.comment(re.getUid() + "用户记录条数：" + count);
			// List<String> CartIdList=new ArrayList<String>();
			// CartIdList.add(re.get_id());
			// Logger.comment("setCartIdList:"+CartIdList);
			// batchbean.setCartIdList(CartIdList);
			Logger.comment("setUserId:" + re.getUid());
			batchbean.setUserId(re.getUid());
			batchcall.setData(batchbean);
			batchcall.callService();
			Logger.comment("delcatcall send:" + batchcall.getEntityString());
			Logger.comment("delcatcall return:" + batchcall.getReturnData());
			Logger.verifyEquals(ECode.ERROR500, batchcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, batchcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, batchcall.getMsg().contains(ECode.ERROR500_MSG_1), "验证返回Msg是否包含字符:"
					+ ECode.ERROR500_MSG_1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchDeleteCatalogFromCart_003() {
		Logger.start(true,"不输入必填项UserId，删除购物车中记录失败");
		try {
			Logger.comment("从mongodb中获取一条购物车记录");

			ShopCartMongoBean re = scw.getOneShoppingCartInfo("{}");
			int count = scw.getShoppingCartCount("{\"uid\":\"" + re.getUid() + "\"}");
			Logger.comment(re.getUid() + "用户记录条数：" + count);
			List<String> CartIdList = new ArrayList<String>();
			CartIdList.add(re.get_id());
			Logger.comment("setCartIdList:" + CartIdList);
			batchbean.setCartIdList(CartIdList);
			// Logger.comment("setUserId:"+re.getUid());
			// batchbean.setUserId(re.getUid());
			batchcall.setData(batchbean);
			batchcall.callService();
			Logger.comment("delcatcall send:" + batchcall.getEntityString());
			Logger.comment("delcatcall return:" + batchcall.getReturnData());
			Logger.verifyEquals(ECode.ERROR500, batchcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, batchcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, batchcall.getMsg().contains(ECode.ERROR500_MSG_2), "验证返回Msg是否包含字符:"
					+ ECode.ERROR500_MSG_2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BatchDeleteCatalogFromCart_004() {
		Logger.start(true,"输入UserId和CartId不匹配，删除购物车中记录失败");
		try {
			Logger.comment("从mongodb中获取一条购物车记录");

			ShopCartMongoBean re = scw.getOneShoppingCartInfo("{}");
			int count = scw.getShoppingCartCount("{\"uid\":\"" + re.getUid() + "\"}");
			Logger.comment(re.getUid() + "用户记录条数：" + count);
			List<String> CartIdList = new ArrayList<String>();
			CartIdList.add(re.get_id());
			Logger.comment("setCartIdList:" + CartIdList);
			batchbean.setCartIdList(CartIdList);
			// 不匹配，这个uid实际不存在
			Logger.comment("setUserId:" + re.getUid() + 1);
			batchbean.setUserId(re.getUid() + 1);
			batchcall.setData(batchbean);
			batchcall.callService();
			Logger.comment("delcatcall send:" + batchcall.getEntityString());
			Logger.comment("delcatcall return:" + batchcall.getReturnData());
			Logger.verifyEquals(ECode.FAILED201, batchcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE2, batchcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, batchcall.getMsg().contains(ECode.FAILED201_MSG_6), "验证返回Msg是否包含字符:"
					+ ECode.FAILED201_MSG_6);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BatchDeleteCatalogFromCart_005() {
		Logger.start(true,"输入不存在CartId，删除购物车中记录失败");
		try {
			Logger.comment("从mongodb中获取一条购物车记录");

			ShopCartMongoBean re = scw.getOneShoppingCartInfo("{}");
			List<String> CartIdList = new ArrayList<String>();
			// 添加一个随机生成的uuid左右cartid
			CartIdList.add(TestDataManager.getRandomUUID().toString());
			Logger.comment("setCartIdList:" + CartIdList);
			batchbean.setCartIdList(CartIdList);
			Logger.comment("setUserId:" + re.getUid());
			batchbean.setUserId(re.getUid());
			batchcall.setData(batchbean);
			batchcall.callService();
			Logger.comment("delcatcall send:" + batchcall.getEntityString());
			Logger.comment("delcatcall return:" + batchcall.getReturnData());
			Logger.verifyEquals(ECode.FAILED201, batchcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE1, batchcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, batchcall.getMsg().contains(ECode.FAILED201_MSG_5), "验证返回Msg是否包含字符:"
					+ ECode.FAILED201_MSG_5);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BatchDeleteCatalogFromCart_006() {
		Logger.start(true,"输入多个CartId，删除购物车中记录成功");
		try {

			String uid = TestDataManager.getRandomUUID().toString();
			int count = ShoppingCartCallServices.addCatalogsToCart(uid, 10);
			Logger.verifyEquals(count, scw.getShoppingCartCount("{\"uid\":\"" + uid + "\"}"), uid + "用户成功添加记录数");
			ArrayList<ShopCartMongoBean> list = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", count);
			List<String> CartIdList = new ArrayList<String>();
			Logger.comment("把所有uid为: " + uid + " 的cartid记录加入list");
			for (ShopCartMongoBean shopCartMongoBean : list) {
				CartIdList.add(shopCartMongoBean.get_id());
			}
			// 全都删除
			Logger.comment("setCartIdList:" + CartIdList);
			batchbean.setCartIdList(CartIdList);
			Logger.comment("setUserId:" + uid);
			batchbean.setUserId(uid);
			batchcall.setData(batchbean);
			batchcall.callService();
			Logger.comment("delcatcall send:" + batchcall.getEntityString());
			Logger.comment("delcatcall return:" + batchcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, batchcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, batchcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(0, scw.getShoppingCartCount("{\"uid\":\"" + uid + "\"}"), "验证删除后记录条数");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BatchDeleteCatalogFromCart_007() {
		Logger.start(true,"输入多个CartId，其中有和UserId不匹配的数据，删除购物车中记录成功");
		try {

			String uid = TestDataManager.getRandomUUID().toString();
			int count = ShoppingCartCallServices.addCatalogsToCart(uid, 10);
			Logger.verifyEquals(count, scw.getShoppingCartCount("{\"uid\":\"" + uid + "\"}"), uid + "用户成功添加记录数");
			ArrayList<ShopCartMongoBean> list = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", count);
			Logger.comment("把所有uid为: " + uid + " 的cartid记录加入list");
			List<String> CartIdList = new ArrayList<String>();
			for (ShopCartMongoBean shopCartMongoBean : list) {
				CartIdList.add(shopCartMongoBean.get_id());
			}
			// 再加一个不匹配数据
			String uid2 = TestDataManager.getRandomUUID().toString();
			int count2 = ShoppingCartCallServices.addCatalogsToCart(uid2, 1);
			Logger.verifyEquals(count2, scw.getShoppingCartCount("{\"uid\":\"" + uid2 + "\"}"), uid2 + "用户成功添加记录数");
			ArrayList<ShopCartMongoBean> list2 = scw.getShoppingCartInfoList("{\"uid\":\"" + uid2 + "\"}", count2);
			Logger.comment("在list中添加UserId为" + uid2 + "的数据" + list2.get(0).get_id());
			CartIdList.add(list2.get(0).get_id());

			Logger.comment("setCartIdList:" + CartIdList);
			batchbean.setCartIdList(CartIdList);
			Logger.comment("setUserId:" + uid);
			batchbean.setUserId(uid);
			batchcall.setData(batchbean);
			batchcall.callService();
			Logger.comment("delcatcall send:" + batchcall.getEntityString());
			Logger.comment("delcatcall return:" + batchcall.getReturnData());
			// 有1个错 的 其他对的。业务逻辑是删除后再查下，所以只要查询到正确输入的CartIdList都删除成功就pass
			Logger.verifyEquals(ECode.FAILED201, batchcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE2, batchcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, batchcall.getMsg().contains(ECode.FAILED201_MSG_6), "验证返回Msg是否包含字符:"
					+ ECode.FAILED201_MSG_6);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BatchDeleteCatalogFromCart_008() {
		Logger.start(true,"输入多个CartId，其中有不存在的CartId数据，删除购物车中记录成功");
		try {

			String uid = TestDataManager.getRandomUUID().toString();
			int count = ShoppingCartCallServices.addCatalogsToCart(uid, 10);
			Logger.verifyEquals(count, scw.getShoppingCartCount("{\"uid\":\"" + uid + "\"}"), uid + "用户成功添加记录数");
			ArrayList<ShopCartMongoBean> list = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", count);
			List<String> CartIdList = new ArrayList<String>();
			Logger.comment("把所有uid为: " + uid + " 的cartid记录加入list");
			for (ShopCartMongoBean shopCartMongoBean : list) {
				CartIdList.add(shopCartMongoBean.get_id());
			}
			Logger.comment("删除第一个记录，再添加一个随机uuid到list");
			CartIdList.remove(0);
			CartIdList.add(TestDataManager.getRandomUUID().toString());
			Logger.comment("setCartIdList:" + CartIdList);
			batchbean.setCartIdList(CartIdList);
			Logger.comment("setUserId:" + uid);
			batchbean.setUserId(uid);
			batchcall.setData(batchbean);
			batchcall.callService();
			Logger.comment("delcatcall send:" + batchcall.getEntityString());
			Logger.comment("delcatcall return:" + batchcall.getReturnData());
			// 有1个错 的 其他对的。业务逻辑是删除后再查下，所以只要查询到正确输入的CartIdList都删除成功就pass
			Logger.verifyEquals(ECode.SUCCESS, batchcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, batchcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(1, scw.getShoppingCartCount("{\"uid\":\"" + uid + "\"}"), "验证删除后记录条数");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}*/
}